package m202406.d28;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/28 22:23
 */
public class Q146 {

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(2);
        lruCache.put(1, 1);
        lruCache.put(2, 2);
        lruCache.get(1);
        lruCache.put(3, 3);
        System.out.println(lruCache.get(2));
    }

    public static class LRUCache {

        private Cache cache;

        public LRUCache(int capacity) {
            this.cache = new Cache(capacity);
        }

        public int get(int key) {
            Integer value = cache.get(key);
            return value == null ? -1 : value;
        }

        public void put(int key, int value) {
            cache.put(key, value);
        }

        private static class Cache extends LinkedHashMap<Integer, Integer> {
            int capacity;

            public Cache(int capacity) {
                super(capacity,0.75f,true);
                this.capacity = capacity;
            }


            @Override
            protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
                return size() > capacity;
            }
        }
    }

}
